//
//  ChangeBankCardControllerViewController.m
//  YiBaoStore
//
//  Created by 吕清毅 on 2018/11/25.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "ChangeBankCardControllerViewController.h"
#import "PickUpViewController.h"

@interface ChangeBankCardControllerViewController ()

@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UITextField *nameTF;
@property (nonatomic,strong) UITextField *accountTF;
@property (nonatomic,strong) UITextField *idCardTF;
@property (nonatomic,strong) UITextField *phoneTF;
@property (nonatomic,strong) UITextField *codeTF;
@property (nonatomic,strong) UIButton *doneBtn;
@property (nonatomic,strong) PickUpViewController *pickVC;
@end

@implementation ChangeBankCardControllerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = HexWithAlpha(0x000000, 0.6);
    [self getData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TextFieldChange) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)getData
{
    [[HttpRequest currentHttpRequest]postPath:RealName_API params:nil resultBlock:^(id Data, MsgModel *msgModel) {
        [self setMainView:Data[@"name"] and:Data[@"idNumber"]];
    }];
}

- (void)setMainView:(NSString *)name and:(NSString*)idCard
{
    CGFloat padd = (SCREENHEIGHT-445)/2;
    _backView = [UIView new];
    _backView.frame = CGRectMake(50, padd, SCREENWIDTH-100, 400);
    _backView.backgroundColor = TotalWhite;
    _backView.layer.cornerRadius = 12;
    [self.view addSubview:_backView];
    
    UIImageView *headIV = [UIImageView new];
    headIV.frame = CGRectMake(20, 14, 28, 28);
    headIV.image = [UIImage imageNamed:@"changeBank"];
    [_backView addSubview:headIV];
    
    UILabel *titleLab = [UILabel new];
    titleLab.frame = CGRectMake(headIV.right+5, 22, 67, 14);
    titleLab.textColor = titleColor;
    titleLab.font = FontSemiboldSize(14);
    titleLab.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:-1 and:@"换绑银行卡"];
    [_backView addSubview:titleLab];
    
    UIImageView *close = [UIImageView new];
    close.frame = CGRectMake(_backView.width-36, 20, 16, 16);
    close.image = [UIImage imageNamed:@"anClose"];
    [_backView addSubview:close];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    closeBtn.frame = CGRectMake(close.left-5, 15, 26, 26);
    [closeBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    [_backView addSubview:closeBtn];
    
    //持卡人姓名
    UILabel *nameLab = [UILabel new];
    nameLab.frame = CGRectMake(leftPadding, headIV.bottom+24, 70, 12);
    [self setLabelAttri:nameLab andStr:@"持卡人姓名"];
    [self.backView addSubview:nameLab];
    
    self.nameTF = [UITextField new];
    self.nameTF.text = name;
    self.nameTF.enabled = NO;
    [self createTFandLineAndDesLab:self.nameTF andFrame:CGRectMake(nameLab.left, nameLab.bottom+12, self.backView.width-40, 15) and:10];
    
    //银行卡账号
    UILabel *bankLab = [UILabel new];
    bankLab.frame = CGRectMake(leftPadding, nameLab.bottom+53, 70, 12);
    [self setLabelAttri:bankLab andStr:@"银行卡账号"];
    [self.backView addSubview:bankLab];
    
    self.accountTF = [UITextField new];
    [self createTFandLineAndDesLab:self.accountTF andFrame:CGRectMake(nameLab.left, bankLab.bottom+12, self.backView.width-40, 15) and:11];
    
    //持卡人身份证号
    UILabel *idCardLab = [UILabel new];
    idCardLab.frame = CGRectMake(leftPadding, bankLab.bottom+53, 100, 12);
    [self setLabelAttri:idCardLab andStr:@"持卡人身份证号"];
    [self.backView addSubview:idCardLab];
    
    self.idCardTF = [UITextField new];
    self.idCardTF.text = idCard;
    self.idCardTF.enabled = NO;
    [self createTFandLineAndDesLab:self.idCardTF andFrame:CGRectMake(nameLab.left, idCardLab.bottom+12, self.backView.width-40, 15) and:12];
    
    //手机号
    UILabel *phoneLab = [UILabel new];
    phoneLab.frame = CGRectMake(leftPadding, idCardLab.bottom+53, 90, 12);
    [self setLabelAttri:phoneLab andStr:@"手机号"];
    [self.backView addSubview:phoneLab];
    
    self.phoneTF = [UITextField new];
    [self createTFandLineAndDesLab:self.phoneTF andFrame:CGRectMake(nameLab.left, phoneLab.bottom+12, self.backView.width-40, 15) and:13];
    
    //验证码
    /*
    UILabel *codeLab = [UILabel new];
    codeLab.frame = CGRectMake(leftPadding, phoneLab.bottom+53, 90, 12);
    [self setLabelAttri:codeLab andStr:@"验证码"];
    [self.backView addSubview:codeLab];
    
    self.codeTF = [UITextField new];
    [self createTFandLineAndDesLab:self.codeTF andFrame:CGRectMake(nameLab.left, codeLab.bottom+12, self.backView.width-40, 15) and:14];
    */
    //确定换绑
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, self.backView.height-44, _backView.width, 44);
    btn.backgroundColor = UIColorFromHex(0xF7D553);
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    btn.titleLabel.font = FontSemiboldSize(12);
    //设置按钮的下部分切圆角
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:btn.layer.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10.f,10.f)];
    CAShapeLayer *maskLayer = [CAShapeLayer new];
    maskLayer.frame = btn.layer.bounds;
    maskLayer.path = maskPath.CGPath;
    btn.layer.mask = maskLayer;
    [btn setAttributedTitle:[TTKUtil getAtttributeStringWithLineSpace:0 kern:-1 and:@"确定换绑"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
    self.doneBtn = btn;
    [self.backView addSubview:btn];
}

- (void)ShowHUDWithMessage:(NSString *)message
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.bezelView.backgroundColor = UIColorFromHex(0xFF9D9D9D);
    hud.contentColor = [UIColor whiteColor];
    hud.label.text = NSLocalizedString(message, nil);
    hud.label.font = FontWithSize(12);
    [hud hideAnimated:YES afterDelay:HUDTime];
}

- (void)TextFieldChange
{
    NSString *idStr = self.idCardTF.text;
    NSString *bankCode = self.accountTF.text;
    NSString *phone = self.phoneTF.text;
    if (idStr.length > 18) {
        self.idCardTF.text = [self.idCardTF.text substringToIndex:18];
    }
    if (bankCode.length > 19) {
        self.accountTF.text = [self.accountTF.text substringToIndex:19];
    }
    if (phone.length > 11) {
        self.phoneTF.text = [self.phoneTF.text substringToIndex:11];
    }
}
- (void)next
{
//    if (self.nameTF.text.length == 0) {
//        [self ShowHUDWithMessage:@"持卡人姓名不能为空"];
//        return;
//    }
    if (self.accountTF.text.length == 0) {
        [self ShowHUDWithMessage:@"银行卡账号不能为空"];
        return;
    }
//    if (self.idCardTF.text.length == 0) {
//        [self ShowHUDWithMessage:@"持卡人身份证不能为空"];
//        return;
//    }
    if (self.phoneTF.text.length == 0) {
        [self ShowHUDWithMessage:@"手机号不能为空"];
        return;
    }
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:self.idCardTF.text forKey:@"bankCardIdNumber"];
    [dict setValue:self.accountTF.text forKey:@"bankCardNumber"];
    [dict setValue:self.nameTF.text forKey:@"bankCardOwnerName"];
    [dict setValue:self.phoneTF.text forKey:@"bankCardReservedMobile"];
    
    [HttpRequest formPostPath:ChangeBankCard_API params:dict resultBlock:^(id Data, MsgModel *msgModel) {
        self.pickVC = [[PickUpViewController alloc]init];
        self.pickVC.bank = @"bank";
        [self.view removeFromSuperview];
        //    [self.view addSubview:self.pickVC.view];
        [[UIApplication sharedApplication].windows[0] addSubview:self.pickVC.view];
        self.pickVC.view.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
    }];
    
    //[self.view removeFromSuperview];
}

#pragma mark -- 获取label
- (void)setLabelAttri:(UILabel *)label andStr:(NSString *)str
{
    label.textColor = titleColor;
    label.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:12];
    label.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:1 and:str];
}

#pragma mark -- 创建tf和des
- (void)createTFandLineAndDesLab:(UITextField*)tf andFrame:(CGRect)frame and:(NSInteger)tag
{
    tf.frame = frame;
    tf.textColor = titleColor;
    tf.font = FontWithSize(15);
    [self.backView addSubview:tf];
    
    UIView *lineView = [UIView new];
    lineView.frame = CGRectMake(tf.left, tf.bottom+4, self.backView.width-40, 1);
    lineView.backgroundColor = UIColorFromHex(0xE3E3E3);
    [self.backView addSubview:lineView];
    if (tag == 13 || tag == 14) {
        tf.keyboardType = UIKeyboardTypeNumberPad;
    }
}



- (void)close
{
    [self.view removeFromSuperview];
}

@end
